Android 11
開始把getExternalStoragePublicDirectory
標記棄用,要求改用MediaStore
,一樣從畫面開始
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CropLensActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.button.MaterialButton
android:id="@+id/aclMbCreatePackageNamePicture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
android:padding="8dp"
android:text="在應用程式資料夾建立照片"
android:textAllCaps="false"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toTopOf="@+id/aclIvPackageNamePicture"
app:layout_constraintDimensionRatio="5:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/aclIvPackageNamePicture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="4dp"
android:layout_marginTop="4dp"
app:layout_constraintBottom_toTopOf="@+id/aclMbCreatePhonePicture"
app:layout_constraintDimensionRatio="4:3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aclMbCreatePackageNamePicture"
tools:src="@tools:sample/backgrounds/scenic" />
<com.google.android.material.button.MaterialButton
android:id="@+id/aclMbCreatePhonePicture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
android:padding="8dp"
android:text="在圖片資料夾建立照片"
android:textAllCaps="false"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toTopOf="@+id/aclIvPhonePicture"
app:layout_constraintDimensionRatio="5:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aclIvPackageNamePicture" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/aclIvPhonePicture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="4dp"
android:layout_marginTop="4dp"
app:layout_constraintDimensionRatio="4:3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aclMbCreatePhonePicture"
tools:src="@tools:sample/backgrounds/scenic" />
<com.google.android.material.button.MaterialButton
android:id="@+id/aclMbCreateMediaStorePicture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
android:padding="8dp"
android:text="MediaStore在圖片資料夾建立照片"
android:textAllCaps="false"
app:autoSizeTextType="uniform"
app:layout_constraintDimensionRatio="5:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aclIvPhonePicture" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/aclIvMediaStorePicture"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="4dp"
android:layout_marginTop="4dp"
app:layout_constraintDimensionRatio="4:3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aclMbCreateMediaStorePicture"
tools:src="@tools:sample/backgrounds/scenic" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
以前使用getExternalStoragePublicDirectory
我們還要自己建立資料夾,使用MediaStore
後直接填入路徑,ContentValues
就會自動幫我們建立資料夾,非常方便。
不過用這種方式儲存檔案,如果遇到同名檔案,會在檔案後面加上(數字)
,例如原本有003.jpg
,下次存檔就會變成003(1).jpg
,以此類推。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_crop_lens)
aclMbCreateMediaStorePicture.setOnClickListener {
val contentValue = ContentValues().apply {
this.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, "003.jpg")
this.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/jpeg")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
this.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, "${Environment.DIRECTORY_PICTURES}/AndroidSystem")
}
}
val uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValue)
takePictureResultLauncher.launch(uri)
}
}